$(function () {
	//加载皮肤设置
	loadSkin($("#userSkin").val());
	//加载菜单
	getMenu();
	
	openMenu('0','menu_0001','menu_0000','欢迎页','/backstage/adv');
	//openMenu('0','menu_0001','menu_0000','欢迎页','/router/customer/index')
	
	//皮肤设置框-主题事件
	$('#skinSetting input[name=skin]').on('click', function(){
		var skin=$(this).val();
		$('body').removeClass().addClass(skin);
		loadSkin(skin);
	});	
	//加载个性化设置
	loadPro();
	//切换窄屏 
	$('#jy-settings-add-container').on('click', function(){
		var checked=this.checked;
		if(checked)$("#main-container").addClass("container");
		else       $("#main-container").removeClass("container");
	});
	//缩放菜单
	$('#jy-settings-sfMenu').on('click', function(){
		var checked=this.checked;
		if(checked) $("#sidebar").addClass("menu-min");
		else        $("#sidebar").removeClass("menu-min");
	});	
	//切换右菜单
	$('#jy-settings-rtl').on('click', function(){
		var checked=this.checked;
		if(checked) $("#indexBody").addClass("rtl");
		else        $("#indexBody").removeClass("rtl");
	});	
	//设置头像
	$('#headpicRecommend li').click(function(){
		$('#headpicRecommend li').removeClass("visiHot");
		$(this).addClass("visiHot");
		$("#headpicPreviewImg").attr('src',$(this).find('img').attr('src')); 
	});
});
function loadSkin(skin){
	$('body').removeClass().addClass(skin);
	var skinColor="";
	if(skin=="skin-0")skinColor="blue";
	else if(skin=="skin-1")skinColor="dark";
	else if(skin=="skin-2")skinColor="pink";
	else if(skin=="skin-3")skinColor="light-grey";		
	$('#skinSettingIcon1').removeClass().addClass(skinColor+" icon-github-alt bigger-110");
	$('#skinSettingIcon2').removeClass().addClass(skinColor+" icon-magnet bigger-110");
}
function loadPro(){
	var settings=$.cookie('JY.settings');
	if(typeof(settings) != "undefined"){
		var sjson = JSON.parse(settings);	
		//切换窄屏 
		if(typeof(sjson.narWinMenu) != "undefined" &&sjson.narWinMenu=="1"){
			$("#main-container").addClass("container");
			$("#jy-settings-add-container").prop("checked",true);			
		}else{
			$("#main-container").removeClass("container");	
			$("#jy-settings-add-container").prop("checked",false);
		}
		//缩放菜单
		if(typeof(sjson.sfMenu) != "undefined" &&sjson.sfMenu=="1"){
			$("#sidebar").addClass("menu-min");
			$("#jy-settings-sfMenu").prop("checked",true);		
		}else{
			$("#sidebar").removeClass("menu-min");	
			$("#jy-settings-sfMenu").prop("checked",false);		
		}
		//切换右菜单
		if(typeof(sjson.posMenu) != "undefined" &&sjson.posMenu=="1"){
			$("#indexBody").addClass("rtl");
			$("#jy-settings-rtl").prop("checked",true);	
		}else{
			$("#indexBody").removeClass("rtl");	
			$("#jy-settings-rtl").prop("checked",false);	
		}
	}else{
		var sjson={};
		$.cookie('JY.settings', JSON.stringify(sjson));
	}
}
function perSetting(){
	$("#perSetting").removeClass('hide').dialog({
		resizable:false,dialogClass:"title-no-close",modal:true,//设置为true，该dialog将会有遮罩层
		title: "<div class='widget-header'><h4 class='font-bold'>系统设置</h4></div>",title_html: true,
		show:{effect:"fade"},
		buttons: [
		  {  
			html: "<i class='icon-ok bigger-110'></i>&nbsp;确认","class" : "btn btn-primary btn-xs",
			click: function() {	
				var that =$(this);
				 JY.Ajax.doRequest("perSettingFrom",jypath +'/system/account/editSkin',null,function(data){
		        	$("#userSkin").val($("#perSettingFrom input:radio[name=skin]:checked").val());
		        	savePro();
		        	that.dialog("close");
		        	JY.Model.info(data.resMsg); 		 
				 }); 	
			}
		  },
		  {
			html: "<i class='icon-remove bigger-110'></i>&nbsp;取消","class" : "btn btn-xs",
			click: function() {		
				//取消恢复皮肤
				var skin=$("#userSkin").val();
				loadSkin(skin);	
				$("#skinSetting input:radio[name=skin][value='"+skin+"']").prop("checked",true);
				//取消恢复个性化
				loadPro();
				$(this).dialog("close");
			}
		  }
		]
	}); 
}
function savePro(){
	var checked=false;
	var sjson = JSON.parse($.cookie('JY.settings'));
	//切换窄屏 
	checked=$("#perSettingFrom input:checkbox[name=narWinMenu]:checked").val();
	if(checked)sjson.narWinMenu="1";
	else       sjson.narWinMenu="0";
	//缩放菜单
	checked=$("#perSettingFrom input:checkbox[name=sfMenu]:checked").val();
	if(checked)sjson.sfMenu="1";
	else       sjson.sfMenu="0";
	//切换右菜单
	checked=$("#perSettingFrom input:checkbox[name=posMenu]:checked").val();
	if(checked)sjson.posMenu="1";
	else       sjson.posMenu="0";
	//保存至cookie
	$.cookie('JY.settings', JSON.stringify(sjson));
}

function perData(){
	//获取个人资料
	JY.Ajax.doRequest(null,jypath +'/system/account/findCurrentUser',null,function(data){
		if(data.res==1){
       		var o=data.obj;
       		//加载个人资料
       		$("#userName").empty().append(JY.Object.notEmpty(o.userName));
       		$("#userPhone").empty().append(JY.Object.notEmpty(o.userPhone));
       		//$("#userAccount").empty().append(JY.Object.notEmpty(o.userAccount));
       		var userLandlines = JY.Object.notEmpty(o.userLandlines);
       		$("label[name='addContantError']").text("");
       		if(userLandlines){
       			var linesObj = eval('(' + userLandlines + ')')
           		if(linesObj.length > 0){
           			$("div .rowCount").remove();
               		for(var ii=0; ii<linesObj.length; ii++){
               			var typeOneSel = "";
               			var typeTwoSel = "";
               			var jyValidateVal = "isMobile";
               			if("1" == linesObj[ii].type){
               				typeOneSel = "selected=\"selected\"";
               			}else if("2" == linesObj[ii].type){
               				typeTwoSel = "selected=\"selected\"";
               				jyValidateVal = "isTel";
               			}
               			var html = "<div class=\"profile-info-value tab-pane-right rowCount\" style=\"margin-left: 130px;\">";
                		html += "<select name=\"contactType\" onchange=\"changeContactType(this);\">";
                		html += "<option value = \"1\" "+typeOneSel+">手机</option>";
                		html += "<option value = \"2\" "+typeTwoSel+">固话</option>";
                		html += "</select>";
                		html += "&nbsp;<input type=\"text\" name=\"phone\" jyValidate=\""+jyValidateVal+"\" value=\""+linesObj[ii].phone+"\" style=\"height: 30px;\"/>";
                		html += "<a href=\"javascript: void(0);\" onclick=\"delRow(this);\">移除</a>";
                		html += "</div>";
                		$("div[name='addContactNumber']").before(html);
               		}
               		
           		}
       		}
       		
       		//打开窗体
       		$("#perData").removeClass('hide').dialog({
       			width:500,
       			resizable: false,dialogClass: "title-no-close",modal: true,//设置为true，该dialog将会有遮罩层
       			title: "<div class='widget-header'><h4 class='font-bold'>个人资料</h4></div>",title_html: true,
       			show:{effect:"fade"},
       			buttons: [
       			  {  
       				html: "<i class='icon-ok bigger-110'></i>&nbsp;确认","class" : "btn btn-primary btn-xs",
       				click: function() {
       					var that =$(this);
       					if($("#pwSetting").hasClass("active")){
       						if(JY.Validate.form("pwSettingFrom")){
       							var opwd=$("#pwSettingFrom input[name='opwd']").val();
       							var npwd=$("#pwSettingFrom input[name='npwd']").val();
       							var qpwd=$("#pwSettingFrom input[name='qpwd']").val();
       							if(opwd!=npwd){
       								if(npwd==qpwd){	
       									JY.Ajax.doRequest(null,jypath +'/system/account/editPwd',{opwd:opwd,npwd:npwd,qpwd:qpwd},function(data){
			       				        	JY.Model.info(data.resMsg,function(){
			       				        	    //跳转退出系统页面
				       				        	window.location.href=jypath+"/system_logout";
			       				        	}); 
			       				        	that.dialog("close");
			       				        	
       									});
           							}else{
           								$("#pwSettingFrom input[name='npwd']").tips({side:1,msg : "您输入的密码与确认密码不一致！",bg :'#FF2D2D',time:1});
           							}
       							}else{
       								$("#pwSettingFrom input[name='npwd']").tips({side:1,msg : "您输入的密码与旧密码不能一致！",bg :'#FF2D2D',time:1});
       							}
       						}
       					}else{
       						if(JY.Validate.form("perfileSettingFrom")){
       							var array = new Array();
       							//遍历联系方式列表
       							var divContactType = $("div .rowCount");
       							divContactType.each(function(){
       								var typeValue = "";
   									var phoneValue = "";
   									var childValue = "";
       								//循环联系方式子数据
   									$(this).children().each(function(){
       									var domName = $(this).attr("name");
       									childValue = $(this).val();
       									if("contactType" == domName){
       										typeValue = childValue;
       									}else if("phone" == domName){
       										phoneValue = childValue;
       									}
       								});
   									//组装联系方式数据并push到数组中
   									var contactObj = {};
       								if(phoneValue){
       									contactObj["type"] = typeValue;
           								contactObj["phone"] = phoneValue;
           								array.push(contactObj);
       								}
       							});
       							JY.Ajax.doRequest(null,jypath +'/system/account/editUserLandLines',{landLines: JSON.stringify(array)},function(data){
	       				        	JY.Model.info(data.resMsg);
	       				        	that.dialog("close");
								});
       							console.log(array);
       						}
       					}
       				}
       			  },
       			  {
       				html: "<i class='icon-remove bigger-110'></i>&nbsp;取消","class" : "btn btn-xs",
       				click: function() {		
       					$(this).dialog("close");
       				}
       			  }
       			]
       		}); 
       	 }else{
       		 JY.Model.error(data.resMsg); 
       	 }   		
	});
}

//打开菜单
function openMenu(type,id,parentId,menuName,resUrl,e){
	    if('0'!=type || "noset"==resUrl)return;
		else if('/'==resUrl.substring(0,1))TabControlAppend(id,menuName,jypath+resUrl+"?menu="+id, "2");
		else TabControlAppend(id,menuName,resUrl+"?menu="+id);	
	    $(document).attr("title",menuName+"-锦艺M2案场管理系统-"+merchantName);
	    var _this = $(e);
	    $('.nav-list>li .submenu>li>a').removeClass('active');
	    _this.addClass('active');
}
function getMenu(layer,ref){
	$("#menu_li_id").empty();
	JY.Ajax.doRequest(null,jypath +'/backstage/menu/getMenu',{layer:layer,ref:ref},function(data){
    	$("#menu_li_id").html(data.obj);
    	
    	$("#menu_li_id li a").click(function(){
    		$(this).parent().addClass("activeNew");
    		$(this).parent().siblings().removeClass("activeNew");
    		$(this).parent().siblings().find(".submenu").hide();
    	})
	});
}
function logout(){
	JY.Model.confirm("确认要退出吗？",function(){window.location.href=jypath+"/system_logout";});	
}

function addRow(){
	var rowCount = $("div .rowCount").length;
	if(rowCount < 5){
		var html = "<div class=\"profile-info-value tab-pane-right rowCount\" style=\"margin-left: 130px;\">";
		html += "<select name=\"contactType\" onchange=\"changeContactType(this);\">";
		html += "<option value = \"1\">手机</option>";
		html += "<option value = \"2\">固话</option>";
		html += "</select>";
		html += "&nbsp;<input type=\"text\" name=\"phone\" jyValidate=\"isMobile\" style=\"height: 30px;\"/>";
		html += "<a href=\"javascript: void(0);\" onclick=\"delRow(this);\">移除</a>";
		html += "</div>";
		$("div[name='addContactNumber']").before(html);
	}else{
		$("label[name='addContantError']").text("△△△最多添加5条联系电话△△△");
	}
}

function delRow(obj){
	$(obj).parent().remove();
	var rowCount = $("div .rowCount").length;
	if(rowCount < 5){
		$("label[name='addContantError']").text("");
	}
}

function changeContactType(obj){
	var contactType = $(obj).val();
	var phoneInput = $(obj).next("input[name='phone']");
	if("1" == contactType){
		phoneInput.attr("jyValidate", "isMobile");
	}else if("2"==contactType){
		phoneInput.attr("jyValidate", "isTel");
	}
}

function appDownloadPage() {
    $("#appDownload").removeClass('hide').dialog({
        resizable:false,dialogClass:"title-no-close",modal:true,//设置为true，该dialog将会有遮罩层
        title: "<div class='widget-header'><h4 class='font-bold'>App下载</h4></div>",title_html: true,
        show:{effect:"fade"},
        width:420,
        buttons: [
            {
                html: "<i class='icon-remove bigger-110'></i>&nbsp;取消","class" : "btn btn-xs",
                click: function() {
                    $(this).dialog("close");
                }
            }
        ]
    });
}
